var mongoose = require("mongoose");
var db = mongoose.connection;
var page = require("../public/page.js");
var articleSchema = new mongoose.Schema({
  //标题
  title:{type:String , required:true},
  //栏目
  column:{type:String , required:true},
  //作者
  author:{type:String , required:true},
  //发布时间
  releaseTime:{type:String , required:true},
  //简介
  briefing:{type:String , required:true},
  //来源
  source:{type:String , default:"www.hxlovexc.com"},
  //内容
  content:{type:String , required:true},
  //评论
  comment : {type : Array}
});
var articleS = db.model("article" , articleSchema);
//方法
function Article(){};
//添加
Article.prototype.addArticle = function(data , callback){
  articleS.create(data , function(error , data){
    if(error){
      return callback("保存失败");
    };
    return callback(null , "发布成功");
  });
};
//编辑
Article.prototype.editArticle = function(data , callback){
  var id = data.id;
  delete data["id"];
  articleS.update({_id:id} , data , function(error , data){
    if(error){
      return callback("保存失败");
    };
    return callback(null , "编辑成功");
  });
};
//获取
Article.prototype.get = function(data , callback){
  page(data , articleS , callback);
};
//获取详情
Article.prototype.getOne = function(data , callback){
  articleS.findOne(data , function(error , data){
    if(error){
      return callback("获取失败");
    }else{
      return callback(null , data);
    };
  });
};
//删除
Article.prototype.del = function(data , callback){
  articleS.remove(data , function(error , data){
    if(error){
      return callback("删除失败");
    }else{
      return callback(null , data);
    };
  })
};
//评论
//添加评论
Article.prototype.setComment = function(id , data , callback){
	articleS.update({_id : id} , {$push : {comment : data}} , function(error , data){
		if(error){
			return callback(error);
		}else{
			return callback(null , data);
		};
	});
};
module.exports = Article;
